home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 March / PCWorld_2007-03_cd.bin / v cisle / httrack / httrack-3.41-2.exe / {app} / libtest / callbacks-example-log.c < prev    next >
C/C++ Source or Header  |  2006-06-05  |  4KB  |  111 lines

  1. /*
  2.     HTTrack external callbacks example : dumy plugin, aimed to log for debugging purpose
  3.  
  4.     How to build: (callback.so or callback.dll)
  5.       With GNU-GCC:
  6.         gcc -O -g3 -Wall -D_REENTRANT -shared -o mycallback.so callbacks-example.c -lhttrack1
  7.       With MS-Visual C++:
  8.         cl -LD -nologo -W3 -Zi -Zp4 -DWIN32 -Fe"mycallback.dll" callbacks-example.c libhttrack1.lib
  9.  
  10.       Note: the httrack library linker option is only necessary when using libhttrack's functions inside the callback
  11.  
  12.     How to use:
  13.       httrack --wrapper mycallback ..
  14. */
  15.  
  16. /* system includes */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20.  
  21. /* standard httrack module includes */
  22. #include "httrack-library.h"
  23. #include "htsopt.h"
  24. #include "htsdefines.h"
  25.  
  26. /* local function called as "check_html" callback */
  27. static int process_file(t_hts_callbackarg *carg, httrackp *opt, 
  28.                         char* html, int len, const char* url_address, const char* url_file) {
  29.   void *ourDummyArg = (void*) CALLBACKARG_USERDEF(carg);    /*optional user-defined arg*/
  30.   char *fmt;
  31.  
  32.   /* call parent functions if multiple callbacks are chained. you can skip this part, if you don't want previous callbacks to be called. */
  33.   if (CALLBACKARG_PREV_FUN(carg, check_html) != NULL) {
  34.     if (!CALLBACKARG_PREV_FUN(carg, check_html)(CALLBACKARG_PREV_CARG(carg), opt,
  35.                                                 html, len, url_address, url_file)) {
  36.         return 0;  /* abort */
  37.       }
  38.   }
  39.  
  40.   /* log */
  41.   fprintf(stderr, "* parsing file %s%s\n", url_address, url_file);
  42.   fmt = malloc(strlen(url_address) + strlen(url_file) + 128);
  43.   sprintf(fmt, " parsing file %s%s", url_address, url_file);
  44.   hts_log(opt, "log-wrapper-info", fmt);
  45.   free(fmt);
  46.  
  47.   return 1;  /* success */
  48. }
  49.  
  50. static int start_of_mirror(t_hts_callbackarg *carg, httrackp *opt) {
  51.   const char *arginfo = (char*) CALLBACKARG_USERDEF(carg);
  52.  
  53.   fprintf(stderr, "* mirror start\n");
  54.   hts_log(opt, arginfo, "mirror started");
  55.  
  56.   /* call parent functions if multiple callbacks are chained. you can skip this part, if you don't want previous callbacks to be called. */
  57.   if (CALLBACKARG_PREV_FUN(carg, end) != NULL) {
  58.     /* status is ok on our side, return other callabck's status */
  59.     return CALLBACKARG_PREV_FUN(carg, start)(CALLBACKARG_PREV_CARG(carg), opt);
  60.   }
  61.  
  62.   return 1;  /* success */
  63. }
  64.  
  65. /* local function called as "end" callback */
  66. static int end_of_mirror(t_hts_callbackarg *carg, httrackp *opt) {
  67.   const char *arginfo = (char*) CALLBACKARG_USERDEF(carg);
  68.  
  69.   fprintf(stderr, "* mirror end\n");
  70.   hts_log(opt, arginfo, "mirror ended");
  71.  
  72.   /* call parent functions if multiple callbacks are chained. you can skip this part, if you don't want previous callbacks to be called. */
  73.   if (CALLBACKARG_PREV_FUN(carg, end) != NULL) {
  74.     /* status is ok on our side, return other callabck's status */
  75.     return CALLBACKARG_PREV_FUN(carg, end)(CALLBACKARG_PREV_CARG(carg), opt);
  76.   }
  77.  
  78.   return 1;  /* success */
  79. }
  80.  
  81. /*
  82. module entry point
  83. the function name and prototype MUST match this prototype
  84. */
  85. EXTERNAL_FUNCTION int hts_plug(httrackp *opt, const char* argv) {
  86.   /* optional argument passed in the commandline we won't be using here */
  87.   const char *arg = strchr(argv, ',');
  88.   if (arg != NULL)
  89.     arg++;
  90.  
  91.   /* plug callback functions */
  92.   if (arg == NULL)
  93.     arg = "log-wrapper-info";
  94.   hts_log(opt, arg, "* plugging functions");
  95.   CHAIN_FUNCTION(opt, check_html, process_file, (char*) arg);
  96.   CHAIN_FUNCTION(opt, start, start_of_mirror, (char*) arg);
  97.   CHAIN_FUNCTION(opt, end, end_of_mirror, (char*) arg);
  98.  
  99.   hts_log(opt, arg, "* module successfully plugged");
  100.   return 1;  /* success */
  101. }
  102.  
  103. /*
  104. module exit point
  105. the function name and prototype MUST match this prototype
  106. */
  107. EXTERNAL_FUNCTION int hts_unplug(httrackp *opt) {
  108.   hts_log(opt, "log-wrapper-info", "* module successfully unplugged");
  109.   return 1;
  110. }
  111.